package org.devastra.arcinspect.plan.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Element;

public class Module {

	private String id;
	private int position;
	private String name;
	private String desc;
	
	//private Map<String, List<String>> modLayerPackages;
	private Map<String, List<String>> layerId2PackageListMap;
	
	public Module(Element _elem) {
		id = _elem.getAttributeValue("id");
		name = _elem.getAttributeValue("name");
		desc = _elem.getAttributeValue("desc");
		name = _elem.getAttributeValue("name");
		layerId2PackageListMap = new HashMap<String, List<String>>();
	}
	
	private Module(String _id, String _name, String _desc, int _pos) {
		id = _id;
		name = _name;
		desc = _desc;
		position = _pos;
		layerId2PackageListMap = new HashMap<String, List<String>>();
	}
	
	public static Module getModule(Element _elem) {
		Module m = null;

		String id = _elem.getAttributeValue("id");
		String name = _elem.getAttributeValue("name");
		String desc = _elem.getAttributeValue("desc");
		String posStr = _elem.getAttributeValue("pos");
		
		if(validateInputs(id, name, desc, posStr)) {
			m = new Module(id, name, desc, Integer.parseInt(posStr));
			//m.updatePackageDetails(_elem);
		} else {
			System.out.println("Error: In module declaration [id:"+id+"]: "+_elem.toString());
		}
		return m;
	}
	
	
	
	public String getId() {
		return id;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	/*
	public Map<String, List<String>> getModLayerPackages() {
		return modLayerPackages;
	}

	public void setModLayerPackages(Map<String, List<String>> modLayerPackages) {
		this.modLayerPackages = modLayerPackages;
	}
	*/

	public void addPackage(String _layerId, String _packageName) {
		List<String> packageNameList = layerId2PackageListMap.get(_layerId);
		if(packageNameList == null) {
			packageNameList = new ArrayList<String>();
			layerId2PackageListMap.put(_layerId, packageNameList);
		}
		packageNameList.add(_packageName);
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("<Module id=\"").append(id)
			.append("\" name=\"").append(name)
			.append("\" desc=\"").append(desc)
			.append("\" pos=\"").append(position)
			.append("\"/>");
		
		return builder.toString();
	}

	/**
	 * Validate all inputs
	 * @param _id
	 * @param _name
	 * @param _desc
	 * @param _pos
	 * @return
	 */
	private static boolean validateInputs(String _id, String _name, String _desc, String _pos) {
		boolean retVal = true;
		
		if(_id == null || _id.trim().length() == 0) {
			retVal = false;
		} else if (_name == null || _name.trim().length() == 0) {
			retVal = false;
		} else if (_pos == null || _pos.trim().length() == 0) {
			retVal = false;
		} else {
			try {
				Integer.parseInt(_pos);
			} catch (NumberFormatException e) {
				retVal = false;
			}
		}
		return retVal;
	}
	
}
